package com.boil.qz.safekavass.shiro.filter;

import com.boil.util.Constants;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.web.servlet.OncePerRequestFilter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * Created by ChenYong on 2017-04-14.
 * <br>
 * 初始化过滤器。
 */
public class InitFilter extends OncePerRequestFilter {
    /**
     * 日志
     */
    private static Logger log = LoggerFactory.getLogger(InitFilter.class);

    @Override
    protected void doFilterInternal(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException {
        // 获取请求
        HttpServletRequest request1 = (HttpServletRequest) request;

        // 当第一个请求来时，立刻缓存服务器的请求路径
        if (StringUtils.isBlank(Constants.SERVER_REQUEST_PATH)) {
            Constants.SERVER_REQUEST_PATH = String.format("%s://%s:%s%s/", //
                    request1.getScheme(), //
                    request1.getServerName(), //
                    request1.getServerPort(), //
                    request1.getContextPath());

            log.info("服务器请求路径：{}", Constants.SERVER_REQUEST_PATH);
        }

        chain.doFilter(request, response);
    }
}